package com.woniuxy.store.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.cloud.mall95.log.Log;
import com.woniuxy.member.client.MemberQueryClient;
import com.woniuxy.member.client.result.MemberClientDto;
import com.woniuxy.reservation.client.param.InsertTakeNumParam;
import com.woniuxy.reservation.client.param.SearchTakeNumByConditionParam;
import com.woniuxy.reservation.client.result.TakeNumClientDto;
import com.woniuxy.reservation.client.takenumclient.TakeNumManageClient;
import com.woniuxy.reservation.client.takenumclient.TakeNumQueryClient;
import com.woniuxy.store.app.util.StoreIdUtil;
import com.woniuxy.store.client.TakeNumClient;
import com.woniuxy.store.client.param.takeNumParam.InsertTakeNumParams;
import com.woniuxy.store.client.param.takeNumParam.SearchTakeNumByConditionParams;
import com.woniuxy.store.service.StoreInfoService;
import common.WoniuPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import result.Result;

import javax.validation.Valid;

@RestController
@RequestMapping("/store/reservation")
public class TakeNumClientImpl implements TakeNumClient {
    @Autowired
    private TakeNumManageClient takeNumManageClient;
    @Autowired
    private TakeNumQueryClient takeNumQueryClient;
    @Autowired
    private StoreInfoService storeInfoService;
    @Autowired
    private MemberQueryClient memberQueryClient;

    @Override
    @Log("添加排队号")
    public Result insertTakeNum(@Valid InsertTakeNumParams var1) {
        InsertTakeNumParam param = BeanUtil.toBean(var1, InsertTakeNumParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        return takeNumManageClient.insertTakeNum(param);
    }

    @Override
    @Log("修改排队号状态")
    public Result updateTakeNumState(Integer var2, String var3) {
        return takeNumManageClient.updateTakeNumState(StoreIdUtil.getFromToken(),var2,var3);
    }

    @Override
    @Log("根据条件查询排队号")
    public Result searchTakeNumByCondition(@Valid SearchTakeNumByConditionParams var1) {
        SearchTakeNumByConditionParam param = BeanUtil.toBean(var1, SearchTakeNumByConditionParam.class);
        param.setStoreId(StoreIdUtil.getFromToken());
        Result<WoniuPage<TakeNumClientDto>> result = takeNumQueryClient.searchTakeNumByCondition(param);
        for (TakeNumClientDto takeNumClientDto : result.getData().getList()) {
            takeNumClientDto.setStoreName(storeInfoService.getById(takeNumClientDto.getStoreId()).getStoreName());
            if (takeNumClientDto.getMemberId()!=null) {
                Result<MemberClientDto> member = memberQueryClient.getMemberByMemberId(takeNumClientDto.getMemberId());
                if (member!=null) {
                    takeNumClientDto.setPhone(member.getData().getPhone());
                }
            }
        }
        return result;
    }
}
